﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKArchMethodLibrary.Utils;
using YJKCodeBase.WinForm;
using YJKElementFinish.ColumnSurface.DataDefinition;
using YJKElementFinish.ColumnSurface.Managers;
using YJKRevitCode.ElmentSearchUtility.LinkDocumentSuite;
using TopologySearch;

namespace YJKElementFinish.WallInsulationLayer.Controllers
{
	public class AddSurfaceLayersController
	{
		public AddSurfaceLayersController(ExternalCommandData cmdData)
		{
			this.m_ComdData = cmdData;
			this.m_UIApp = cmdData.Application;
			this.m_UIDoc = this.m_UIApp.ActiveUIDocument;
			this.m_App = this.m_UIApp.Application;
			this.m_Doc = this.m_UIDoc.Document;
		}

		public Result AddSurfaceByRefCurve(List<WallTypeLayerInfos> currentWallTypeLayerInfo)
		{
			List<ReferenceCurveInfo> refCurves = new List<ReferenceCurveInfo>();
			this.GetRefCurves(this.m_Doc, ref refCurves);
			if (!this.GetWallInfosByRefCurve(ref refCurves))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			List<ColumnEdgeLoopInfo> currentLayerColumnInfos = new List<ColumnEdgeLoopInfo>();
			if (!this.GetColumnInfosByRefCurve(ref refCurves, ref currentLayerColumnInfos))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			this.CreateOffsetWalls(refCurves, currentLayerColumnInfos, currentWallTypeLayerInfo);
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void GetRefCurves(Document doc, ref List<ReferenceCurveInfo> refCurves)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc, doc.ActiveView.Id);
			ICollection<Element> collection = filteredElementCollector.OfClass(typeof(CurveElement)).ToElements();
			foreach (Element element in collection)
			{
				if (!(element.GetType() != typeof(DetailLine)) || !(element.GetType() != typeof(DetailArc)))
				{
					DetailCurve detailCurve = element as DetailCurve;
					if (detailCurve.LineStyle.Name.IndexOf(PositioningCurveController.m_StrLineNamePrefix) != -1)
					{
						Curve curve = detailCurve.GeometryCurve;
						XYZ endPoint = curve.GetEndPoint(0);
						XYZ xyz = new XYZ(0.0, 0.0, -endPoint.Z);
						Transform transform = Transform.CreateTranslation(xyz);
						curve = RevitVersionFuncs.CreateTransformed(transform, curve);
						ReferenceCurveInfo item = new ReferenceCurveInfo(curve, element.Id);
						refCurves.Add(item);
					}
				}
			}
		}

		private bool GetWallInfosByRefCurve(ref List<ReferenceCurveInfo> refCurves)
		{
			bool result;
			try
			{
				List<WallGeometryInfo> list = new List<WallGeometryInfo>();
				FilteredElementCollectorLinkDocument filteredCollector = this.GetFilteredCollector(true);
				ElementClassFilter filter = new ElementClassFilter(typeof(Wall));
				List<BaseLinkDocumentResultBean> list2 = filteredCollector.WherePass(filter).ToList<BaseLinkDocumentResultBean>();
				for (int i = 0; i < list2.Count; i++)
				{
					BaseLinkDocumentResultBean baseLinkDocumentResultBean = list2[i];
					if (baseLinkDocumentResultBean != null && baseLinkDocumentResultBean.UseElement is Wall)
					{
						Wall wall = baseLinkDocumentResultBean.UseElement as Wall;
						List<Solid> elementSolids = ElementCommon.GetElementSolids(this.m_UIApp, wall);
						CurveArray curveArray = new CurveArray();
						ElementCommon.GetSolidProjection(this.m_Doc, elementSolids, ref curveArray);
						CurveArray curveArray2 = new CurveArray();
						Transform totalTransform = baseLinkDocumentResultBean.GetTotalTransform();
						foreach (object obj in curveArray)
						{
							Curve curve = (Curve)obj;
							curveArray2.Append(curve.CreateTransformed(totalTransform));
						}
						WallGeometryInfo item = new WallGeometryInfo(wall, curveArray2);
						list.Add(item);
					}
				}
				foreach (ReferenceCurveInfo referenceCurveInfo in refCurves)
				{
					Curve theCurve = referenceCurveInfo.TheCurve;
					List<Wall> wallByRefCurve = this.GetWallByRefCurve(theCurve, list);
					foreach (Wall wall2 in wallByRefCurve)
					{
						referenceCurveInfo.AddRefWall(wall2);
						foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean2 in list2)
						{
							if (wall2.Id.IntegerValue == baseLinkDocumentResultBean2.ElementId)
							{
								referenceCurveInfo.TheTransform = baseLinkDocumentResultBean2.GetTotalTransform();
							}
						}
					}
				}
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private bool GetColumnInfosByRefCurve(ref List<ReferenceCurveInfo> refCurves, ref List<ColumnEdgeLoopInfo> currentLayerColumnInfos)
		{
			bool result;
			try
			{
				FilteredElementCollectorLinkDocument filteredCollector = this.GetFilteredCollector(true);
                LogicalOrFilter filter = new LogicalOrFilter(new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumns), new ElementCategoryFilter((BuiltInCategory)(-2000100)));
				List<BaseLinkDocumentResultBean> list = filteredCollector.WherePass(filter).ToList<BaseLinkDocumentResultBean>();
				foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean in list)
				{
					FamilyInstance column = baseLinkDocumentResultBean.UseElement as FamilyInstance;
					ColumnEdgeLoopInfo item = this.CreateByColumn(this.m_UIApp, column, baseLinkDocumentResultBean.GetTotalTransform());
					currentLayerColumnInfos.Add(item);
				}
				foreach (ReferenceCurveInfo referenceCurveInfo in refCurves)
				{
					Curve theCurve = referenceCurveInfo.TheCurve;
					List<FamilyInstance> columnByRefCurve = this.GetColumnByRefCurve(theCurve, currentLayerColumnInfos);
					foreach (FamilyInstance familyInstance in columnByRefCurve)
					{
						referenceCurveInfo.AddRefColumn(familyInstance);
						foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean2 in list)
						{
							if (familyInstance.Id.IntegerValue == baseLinkDocumentResultBean2.ElementId)
							{
								referenceCurveInfo.TheTransform = baseLinkDocumentResultBean2.GetTotalTransform();
							}
						}
					}
				}
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public ColumnEdgeLoopInfo CreateByColumn(UIApplication UIApp, FamilyInstance column, Transform transform)
		{
			List<ProfileInfo> columnProfileLoopList = new List<ProfileInfo>();
			List<Pole2D> list = new List<Pole2D>();
			List<Solid> elementSolids = ElementCommon.GetElementSolids(UIApp, column);
			foreach (Solid solid in elementSolids)
			{
				List<Pole2D> solidProfile = ColumnCommon.GetSolidProfile(solid, column.Id, 0.0, ref columnProfileLoopList);
				foreach (Pole2D pole2D in solidProfile)
				{
					if (transform != null)
					{
						pole2D.SetCurvePointByTransform(transform);
					}
				}
				list.AddRange(solidProfile);
			}
			return new ColumnEdgeLoopInfo(column, list, columnProfileLoopList);
		}

		private List<Wall> GetWallByRefCurve(Curve detailCurve, List<WallGeometryInfo> CurrentLayerWallInfos)
		{
			List<Wall> list = new List<Wall>();
			foreach (WallGeometryInfo wallGeometryInfo in CurrentLayerWallInfos)
			{
				if (wallGeometryInfo.IsWallFace(detailCurve))
				{
					list.Add(wallGeometryInfo.Component);
				}
			}
			return list;
		}

		private List<FamilyInstance> GetColumnByRefCurve(Curve detailCurve, List<ColumnEdgeLoopInfo> CurrentLayerColumnInfos)
		{
			List<FamilyInstance> list = new List<FamilyInstance>();
			foreach (ColumnEdgeLoopInfo columnEdgeLoopInfo in CurrentLayerColumnInfos)
			{
				if (columnEdgeLoopInfo.IsColumnFace(detailCurve))
				{
					list.Add(columnEdgeLoopInfo.Component);
				}
			}
			return list;
		}

		private void GetOffsetReferenceInfos(Pole2D pole, double offset, ReferenceCurveInfo refCurveInfo, List<ColumnEdgeLoopInfo> currentLayerColumnInfos, ref List<OffsetReferenceInfo> offsetLoopInfos)
		{
			try
			{
				Curve curve = pole.NegativeCurve();
				XYZ xyz = curve.Evaluate(0.5, true);
				Curve curve2 = pole.PositiveCurve();
				XYZ xyz2 = curve2.Evaluate(0.5, true);
				if (refCurveInfo.RefWalls.Count > 0)
				{
					Wall wall = this.SelecteRefWall(refCurveInfo.RefWalls, refCurveInfo.TheCurve);
					Pole2D referencePole = new Pole2D(wall.Id, pole.StartPoint, pole.EndPoint, pole.Bulge, pole.PositiveStartPoint, pole.PositiveEndPoint, pole.NegativeStartPoint, pole.NegativeEndPoint, pole.IsPositiveDirection);
					OffsetReferenceInfo offsetReferenceInfo = new OffsetReferenceInfo(referencePole, null, null);
					Curve curve3 = (wall.Location as LocationCurve).Curve;
					if (refCurveInfo.TheTransform != null)
					{
						curve3 = curve3.CreateTransformed(refCurveInfo.TheTransform);
					}
					if (this.IsParallel(curve3, refCurveInfo.TheCurve))
					{
						double val = curve3.Distance(xyz);
						double val2 = curve3.Distance(xyz2);
						if (Geometry.LessThan(val2, val))
						{
							offsetReferenceInfo.OffsetNegativeBaseline = curve;
						}
						else
						{
							offsetReferenceInfo.OffsetPositiveBaseline = curve2;
						}
						offsetLoopInfos.Add(offsetReferenceInfo);
					}
					else
					{
						XYZ xyz3 = curve3.Evaluate(0.5, true);
						double val3 = xyz3.DistanceTo(xyz);
						double val4 = xyz3.DistanceTo(xyz2);
						if (Geometry.LessThan(val4, val3))
						{
							offsetReferenceInfo.OffsetNegativeBaseline = curve;
						}
						else
						{
							offsetReferenceInfo.OffsetPositiveBaseline = curve2;
						}
						offsetLoopInfos.Add(offsetReferenceInfo);
					}
				}
				else if (refCurveInfo.RefColumns.Count > 0)
				{
					FamilyInstance column = refCurveInfo.RefColumns[0];
					Pole2D referencePole2 = new Pole2D(column.Id, pole.StartPoint, pole.EndPoint, pole.Bulge, pole.PositiveStartPoint, pole.PositiveEndPoint, pole.NegativeStartPoint, pole.NegativeEndPoint, pole.IsPositiveDirection);
					OffsetReferenceInfo offsetReferenceInfo2 = new OffsetReferenceInfo(referencePole2, null, null);
					int num = currentLayerColumnInfos.FindIndex((ColumnEdgeLoopInfo item) => item.Component.Id.IntegerValue == column.Id.IntegerValue);
					if (num != -1)
					{
						ColumnEdgeLoopInfo columnEdgeLoopInfo = currentLayerColumnInfos[num];
						if (!columnEdgeLoopInfo.IsInside(xyz, true))
						{
							offsetReferenceInfo2.OffsetNegativeBaseline = curve;
						}
						else
						{
							offsetReferenceInfo2.OffsetPositiveBaseline = curve2;
						}
					}
					offsetLoopInfos.Add(offsetReferenceInfo2);
				}
			}
			catch (Exception)
			{
			}
		}

		private Wall SelecteRefWall(List<Wall> refWalls, Curve refCurve)
		{
			foreach (Wall wall in refWalls)
			{
				Curve curve = (wall.Location as LocationCurve).Curve;
				if (this.IsParallel(curve, refCurve))
				{
					return wall;
				}
			}
			return refWalls[0];
		}

		private bool IsParallel(Curve curve1, Curve curve2)
		{
			if (curve1.GetType() != curve2.GetType())
			{
				return false;
			}
			if (curve1.GetType() == typeof(Line))
			{
				XYZ a = curve1.GetEndPoint(0) - curve1.GetEndPoint(1);
				XYZ b = curve2.GetEndPoint(0) - curve2.GetEndPoint(1);
				return Geometry.IsParallel(a, b, 1E-09);
			}
			if (curve1.GetType() == typeof(Arc))
			{
				XYZ center = ((Arc)curve1).Center;
				XYZ center2 = ((Arc)curve2).Center;
				return Geometry.IsEqual(center, center2, 0.032);
			}
			return false;
		}

		private void CreateOffsetWalls(List<ReferenceCurveInfo> refCurves, List<ColumnEdgeLoopInfo> currentLayerColumnInfos, List<WallTypeLayerInfos> surfaceLayerInfosList)
		{
			List<OffsetWallInfo> offsetWallInfoList = new List<OffsetWallInfo>();
			TransactionGroup transactionGroup = new TransactionGroup(this.m_Doc);
			transactionGroup.Start("create offset wall");
			try
			{
				double num = 0.0;
				foreach (WallTypeLayerInfos wallTypeLayerInfos in surfaceLayerInfosList)
				{
					double width = wallTypeLayerInfos.Width;
					num += width / 2.0;
					List<CompoundStructureLayer> list = new List<CompoundStructureLayer>();
					foreach (WallLayerInfos wallLayerInfos in wallTypeLayerInfos.LayerList)
					{
						list.Add(wallLayerInfos.StructureLayer);
					}
					Transaction transaction = new Transaction(this.m_Doc);
					transaction.Start("create wall type");
					WallType wallType = null;
					try
					{
						wallType = WallTypeManager.GetWallType(wallTypeLayerInfos.TypeName, null, list, this.m_Doc, false);
						transaction.Commit();
					}
					catch (Exception)
					{
						transaction.RollBack();
						num += width / 2.0;
						continue;
					}
					if (wallType == null)
					{
						num += width / 2.0;
					}
					else
					{
						List<Pole2D> list2 = new List<Pole2D>();
						this.GetOffsetWallInfos(refCurves, num, ref list2);
						StructureLoopSearcher structureLoopSearcher = new StructureLoopSearcher(this.m_ComdData);
						structureLoopSearcher.FormatDoubleLine(ref list2);
						List<OffsetReferenceInfo> realOffsetLoopInfos = new List<OffsetReferenceInfo>();
						using (List<Pole2D>.Enumerator enumerator3 = list2.GetEnumerator())
						{
							while (enumerator3.MoveNext())
							{
								Pole2D pole = enumerator3.Current;
								int num2 = refCurves.FindIndex((ReferenceCurveInfo item) => item.CurveId.IntegerValue == pole.ComponentId.IntegerValue);
								if (num2 != -1)
								{
									ReferenceCurveInfo refCurveInfo = refCurves[num2];
									this.GetOffsetReferenceInfos(pole, num, refCurveInfo, currentLayerColumnInfos, ref realOffsetLoopInfos);
								}
							}
						}
						transaction = new Transaction(this.m_Doc);
						transaction.Start("create wall");
						try
						{
							this.CreateWalls(realOffsetLoopInfos, wallType, ref offsetWallInfoList);
							transaction.Commit();
						}
						catch (Exception)
						{
							transaction.RollBack();
							continue;
						}
						num += width / 2.0;
					}
				}
				transactionGroup.Commit();
				WallCommon.JoinWalls(offsetWallInfoList, this.m_Doc, false);
			}
			catch (Exception)
			{
				if (transactionGroup != null)
				{
					transactionGroup.RollBack();
				}
			}
		}

		private void GetOffsetWallInfos(List<ReferenceCurveInfo> refCurves, double offset, ref List<Pole2D> poleList)
		{
			foreach (ReferenceCurveInfo referenceCurveInfo in refCurves)
			{
				Pole2D item = Pole2D.TransformWallToStructure2DPole(referenceCurveInfo.TheCurve, referenceCurveInfo.CurveId, offset, offset);
				poleList.Add(item);
			}
		}

		private void CreateWalls(List<OffsetReferenceInfo> realOffsetLoopInfos, WallType wallType, ref List<OffsetWallInfo> offsetWallInfoList)
		{
			FilteredElementCollectorLinkDocument filteredCollector = this.GetFilteredCollector(false);
			ElementClassFilter filter = new ElementClassFilter(typeof(Level));
			List<BaseLinkDocumentResultBean> list = filteredCollector.WherePass(filter).ToList<BaseLinkDocumentResultBean>();
			List<Level> list2 = new List<Level>();
			foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean in list)
			{
				list2.Add(baseLinkDocumentResultBean.UseElement as Level);
			}
			FilteredElementCollectorLinkDocument filteredCollector2 = this.GetFilteredCollector(true);
			ElementClassFilter filter2 = new ElementClassFilter(typeof(Wall));
			List<BaseLinkDocumentResultBean> list3 = filteredCollector2.WherePass(filter2).ToList<BaseLinkDocumentResultBean>();
			filteredCollector2 = this.GetFilteredCollector(true);
            LogicalOrFilter filter3 = new LogicalOrFilter(new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumns), new ElementCategoryFilter((BuiltInCategory)(-2000100)));
			list3.AddRange(filteredCollector2.WherePass(filter3).ToList<BaseLinkDocumentResultBean>());
			using (List<OffsetReferenceInfo>.Enumerator enumerator2 = realOffsetLoopInfos.GetEnumerator())
			{
				while (enumerator2.MoveNext())
				{
					OffsetReferenceInfo info = enumerator2.Current;
					try
					{
						OffsetWallInfo offsetWallInfo = null;
						int num = offsetWallInfoList.FindIndex((OffsetWallInfo item) => item.ReferencePole.SameAs(info.ReferencePole));
						if (num == -1)
						{
							offsetWallInfo = new OffsetWallInfo(info.ReferencePole);
							offsetWallInfoList.Add(offsetWallInfo);
						}
						else
						{
							offsetWallInfo = offsetWallInfoList[num];
						}
						Element element = null;
						foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean2 in list3)
						{
							if (baseLinkDocumentResultBean2.ElementId == info.ReferencePole.ComponentId.IntegerValue)
							{
								element = baseLinkDocumentResultBean2.UseElement;
								break;
							}
						}
						double height = 0.0;
						if (element.GetType() == typeof(Wall))
						{
							Wall wall = element as Wall;
							double num2 = 0.0;
							WallCommon.GetWallElevationInfo(wall, list2, ref num2, ref height);
						}
						else
						{
							FamilyInstance column = element as FamilyInstance;
							double num3 = 0.0;
							ColumnCommon.GetColumnElevationInfo(column, list2, ref num3, ref height);
						}
						if (info.OffsetNegativeBaseline != null)
						{
							Wall wall2 = WallCommon.CreateWall(this.m_Doc, info.OffsetNegativeBaseline, wallType, height);
							wall2.Flip();
							offsetWallInfo.AddNegativeWalls(wall2);
						}
						if (info.OffsetPositiveBaseline != null)
						{
							Wall wall3 = WallCommon.CreateWall(this.m_Doc, info.OffsetPositiveBaseline, wallType, height);
							wall3.Flip();
							offsetWallInfo.AddPositiveWalls(wall3);
						}
					}
					catch (Exception ex)
					{
						string message = ex.Message;
					}
				}
			}
		}

		public bool SelectExternalProfileByLinkDoc(ref List<Curve> selFaceProfile, bool bSupportRevitLinkDoc = false)
		{
			bool result;
			try
			{
				CurveArray externalCurves = this.GetExternalCurves(bSupportRevitLinkDoc);
				List<Pole2D> list = new List<Pole2D>();
				foreach (object obj in externalCurves)
				{
					Curve curve = (Curve)obj;
					Pole2D item = Pole2D.TransformWallToStructure2DPole(curve, ElementId.InvalidElementId, 0.0, 0.0);
					list.Add(item);
				}
				StructureLoopSearcher structureLoopSearcher = new StructureLoopSearcher(this.m_ComdData, 0.001);
				List<List<Curve>> fullFloorEdgeLoops = structureLoopSearcher.GetFullFloorEdgeLoops(list, StructureLoopSearcher.SearchEdgeLocation.SEL_Outside);
				foreach (List<Curve> collection in fullFloorEdgeLoops)
				{
					selFaceProfile.AddRange(collection);
				}
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private CurveArray GetExternalCurves(bool bSupportRevitLinkDoc)
		{
			CurveArray result = new CurveArray();
			if (bSupportRevitLinkDoc)
			{
				this.GetExternalCurvesByRevitLinkDoc(ref result);
			}
			else
			{
				this.GetExternalCurvesByCurrentDoc(ref result);
			}
			return result;
		}

		private void GetExternalCurvesByRevitLinkDoc(ref CurveArray curveAryRet)
		{
			FilteredElementCollectorLinkDocument filteredCollector = this.GetFilteredCollector(true);
			ElementClassFilter filter = new ElementClassFilter(typeof(Wall));
			List<BaseLinkDocumentResultBean> lstBeans = filteredCollector.WherePass(filter).ToList<BaseLinkDocumentResultBean>();
			filteredCollector = this.GetFilteredCollector(true);
            LogicalOrFilter filter2 = new LogicalOrFilter(new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumns), new ElementCategoryFilter((BuiltInCategory)(-2000100)));
			List<BaseLinkDocumentResultBean> lstBeans2 = filteredCollector.WherePass(filter2).ToList<BaseLinkDocumentResultBean>();
			this.GetCurveAry(lstBeans, ref curveAryRet);
			this.GetCurveAry(lstBeans2, ref curveAryRet);
		}

		private void GetExternalCurvesByCurrentDoc(ref CurveArray curveAryRet)
		{
			List<Wall> list = new List<Wall>();
			WallCommon.GetWalls(this.m_Doc, ref list);
			List<FamilyInstance> list2 = new List<FamilyInstance>();
			ColumnCommon.GetColumns(this.m_Doc, ref list2);
			List<Solid> list3 = new List<Solid>();
			foreach (Wall element in list)
			{
				List<Solid> elementSolids = ElementCommon.GetElementSolids(this.m_UIApp, element);
				list3.AddRange(elementSolids);
			}
			foreach (FamilyInstance element2 in list2)
			{
				List<Solid> elementSolids2 = ElementCommon.GetElementSolids(this.m_UIApp, element2);
				list3.AddRange(elementSolids2);
			}
			ElementCommon.GetSolidProjection(this.m_Doc, list3, ref curveAryRet);
		}

		private void GetCurveAry(List<BaseLinkDocumentResultBean> lstBeans, ref CurveArray curveAryRet)
		{
			foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean in lstBeans)
			{
				List<Solid> list = new List<Solid>();
				List<Solid> elementSolids = ElementCommon.GetElementSolids(this.m_UIApp, baseLinkDocumentResultBean.UseElement);
				list.AddRange(elementSolids);
				CurveArray curveArray = new CurveArray();
				ElementCommon.GetSolidProjection(this.m_Doc, list, ref curveArray);
				Transform totalTransform = baseLinkDocumentResultBean.GetTotalTransform();
				foreach (object obj in curveArray)
				{
					Curve curve = (Curve)obj;
					curveAryRet.Append(curve.CreateTransformed(totalTransform));
				}
			}
		}

		private FilteredElementCollectorLinkDocument GetFilteredCollector(bool bCurrentView = true)
		{
			FilteredElementCollectorLinkDocument filteredElementCollectorLinkDocument = new FilteredElementCollectorLinkDocument(this.m_Doc);
			if (bCurrentView)
			{
				List<Level> sortLevels = Common.GetSortLevels(this.m_Doc, double.MinValue, double.MaxValue);
				Level genLevel = this.m_Doc.ActiveView.GenLevel;
				double elevation = genLevel.Elevation;
				double num = 0.0;
				int i = 0;
				while (i < sortLevels.Count)
				{
					Level level = sortLevels[i];
					if (genLevel.Id.IntegerValue == level.Id.IntegerValue)
					{
						if (i == sortLevels.Count - 1)
						{
							num = elevation + Common.MMToFeet(3000.0);
							break;
						}
						num = sortLevels[i + 1].Elevation;
						break;
					}
					else
					{
						i++;
					}
				}
				XYZ xyz = this.m_Doc.ActiveView.CropBox.Min - new XYZ(10000000.0, 10000000.0, 0.0);
				XYZ xyz2 = this.m_Doc.ActiveView.CropBox.Max + new XYZ(10000000.0, 10000000.0, 0.0);
				XYZ xyz3 = new XYZ(xyz.X, xyz.Y, elevation + Common.MMToFeet(1.0));
				XYZ xyz4 = new XYZ(xyz2.X, xyz2.Y, num - Common.MMToFeet(1.0));
				Outline outline = new Outline(xyz3, xyz4);
				BoundingBoxIntersectsFilter input = new BoundingBoxIntersectsFilter(outline);
				filteredElementCollectorLinkDocument.AddViewLimit(input);
			}
			return filteredElementCollectorLinkDocument;
		}

		private ExternalCommandData m_ComdData;

		private UIApplication m_UIApp;

		private UIDocument m_UIDoc;

		private Autodesk.Revit.ApplicationServices.Application m_App;

		private Document m_Doc;

		public static WindowHandle _hWndRevit;
	}
}
